<?php

namespace App\Http\Controllers\Admin;

use App\Models\Banner;
use App\Validate\BannerValidate;
use Illuminate\Support\Facades\DB;

/**
 * banner 设置
 * Class Banner
 * @package app\api\controller
 */
class BannerController extends CommonController
{
    public $model;
    public $validate = null;


    public function __construct()
    {
        parent::__construct();

        $this->model = new Banner();
        $this->validate = new BannerValidate();
    }


    /**
     * 列表
     * @param page 页数
     * @param limit int 分页大小
     * @param is_play int 是否发布 1.发布 2.未发布    默认1
     * @param big_type_id 空则表示全显示
     * @param keywords string 搜索关键词(文章标题)
     * @param skip_way 跳转方式  1、H5链接，2、每日荐读  3、阅读书单  4、资源详情  5、不跳转
     */
    public function lists()
    {
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? $this->request->limit : 10;
        $keywords = $this->request->keywords;
        $big_type_id = $this->request->big_type_id;
        $is_play = $this->request->is_play;
        $skip_way = $this->request->skip_way;

        $res = $this->model->lists(null, $keywords, $big_type_id, $is_play, $skip_way, $limit);

        if (!$res['data']) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);

        return $this->returnApi(200, "查询成功", true, $res);
    }


    /**
     * 详情
     * @param id int
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->detail($this->request->id);

        if (!$res) {
            return $this->returnApi(201, "参数传递错误");
        }

        return $this->returnApi(200, "查询成功", true, $res);
    }

    /**
     * 新增
     * @param name string 名称
     * @param img string 图片
     * @param link string 链接对应 skip_way的值
     * @param big_type_id init 显示位置，大类
     * @param sort 排序  数字越大，排在越前面
     * @param skip_way 跳转方式  1、H5链接，2、每日荐读  3、阅读书单  4、资源详情  5、不跳转
     * @param intro 简介
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        if (($this->request->skip_way && $this->request->skip_way != 5) && empty($this->request->link)) {
            return $this->returnApi(201,  '跳转方式地址不能为空');
        }
        if ($this->request->big_type_id == 999) {
            return $this->returnApi(201,  '此类型不允许设置');
        }
        $is_exists = $this->model->nameIsExists($this->request->name, 'name');
        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }
        $this->request->merge(['sort' => $this->model->max('sort') + 1]);
        $res = $this->model->add($this->request->all());

        if (!$res) {
            return $this->returnApi(202, "新增失败");
        }
        return $this->returnApi(200, "新增成功", true);
    }


    /**
     * 修改
     * @param name string 名称
     * @param img string 图片
     * @param link string 链接对应 skip_way的值
     * @param big_type_id init 显示位置，大类
     * @param sort 排序  数字越大，排在越前面
     * @param skip_way 跳转方式  1、H5链接，2、每日荐读  3、阅读书单  4、资源详情  5、不跳转
     * @param intro 简介
     */
    public function change()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        if (($this->request->skip_way && $this->request->skip_way != 5) && empty($this->request->link)) {
            return $this->returnApi(201,  '跳转方式地址不能为空');
        }
        if ($this->request->big_type_id == 999) {
            return $this->returnApi(201,  '此类型不允许设置');
        }
        $is_exists = $this->model->nameIsExists($this->request->name, 'name', $this->request->id);
        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }
        $res = $this->model->change($this->request->all());
        if (!$res) {
            return $this->returnApi(202, "修改失败");
        }
        return $this->returnApi(200, "修改成功", true);
    }

    /**
     * 删除
     * @param id int
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $res = $this->model->del($this->request->id);

        if ($res === true) {
            $this->deleteFile($this->model->img); //删除图片
            return $this->returnApi(200, "删除成功", true);
        }
        return $this->returnApi(202, $res);
    }
    /**
     * 撤销 和 发布
     * @param ids int 资源id 对个逗号拼接
     * @param is_play int 发布   1 发布  2 撤销
     */
    public function cancelAndRelease()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('cancel_and_release')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        DB::beginTransaction();
        try {
            list($code, $msg) = $this->model->resumeAndCancel($this->request->ids, 'is_play', $this->request->is_play);

            $is_play = $this->request->is_play == 1 ? '发布' : '撤销';

            DB::commit();
            return $this->returnApi($code, $is_play . $msg, $code === 200 ? true : false);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }

    /**
     * banner 图排序
     * content json格式数据  [{"id":1,"sort":2},{"id":2,"sort":2},{"id":3,"sort":3},{"id":4,"sort":4},{"id":5,"sort":5}]   第一个 sort 最大
     */
    public function sortChange()
    {
        // $data = [
        //   ['id'=>1 , 'sort'=>2],
        //   ['id'=>2 , 'sort'=>2],
        //   ['id'=>3 , 'sort'=>3],
        //   ['id'=>4 , 'sort'=>4],
        //   ['id'=>5 , 'sort'=>5],
        // ];
        // dump(json_encode($data));die;
        //增加验证场景进行验证
        if (!$this->validate->scene('sort_change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $content = $this->request->input('content', '');
        if (empty($content)) {
            return $this->returnApi(201, "参数传递错误");
        }
        $content = json_decode($content, true);
        if (empty($content)) {
            return $this->returnApi(201, "参数传递错误");
        }

        DB::beginTransaction();
        try {
            foreach ($content as $key => $val) {
                $this->model->where('id', $val['id'])->update(['sort' => $val['sort']]);
            }
            DB::commit();
            return $this->returnApi(200, "修改成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }
}
